﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace DataConDemoWebRole.Business
{

    /// <summary>
    /// When an error occurs in a method returning JSON data, we always use
    /// a data object with an error message called "error". By applying
    /// this attribute, any ajax query method will return an error object
    /// instead of the default error page.
    /// </summary>
    public class HandleAjaxError : ActionFilterAttribute, IExceptionFilter
    {
        public void OnException(ExceptionContext filterContext)
        {
            var jr = new JsonResult();
            jr.Data = new JsonError { error = filterContext.Exception.Message };
            filterContext.ExceptionHandled = true;
            filterContext.Result = jr;
        }

        private class JsonError
        {
            public string error { get; set; }
        }
    }
}